package com.wangp.mywrite.coding;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description: 最下绝对差
 * @author tooStronger
 * @date 2022/7/4
 */
public class MinimumAbsDifference {

    public List<List<Integer>> minimumAbsDifference(int[] arr) {
        List<List<Integer>> ans = new ArrayList<>();

        int minDiffer = Integer.MAX_VALUE;
        Arrays.sort(arr);
        for (int i = 0; i < arr.length-1; i++) {
            int abs = Math.abs(arr[i] - arr[i+1]);
            if (abs == minDiffer) {
                ans.add(addSortList(arr[i], arr[i+1]));
            } else if (abs < minDiffer) {
                minDiffer = abs;
                ans.clear();
                ans.add(addSortList(arr[i], arr[i+1]));
            }
        }
        return ans;
    }

    private List<Integer> addSortList(int a, int b) {
        if (a > b) {
            return Arrays.asList(b, a);
        } else {
            return Arrays.asList(a, b);
        }
    }
}
